package com.servlet;

import com.dao.PMationDao;
import com.dao.impl.PMationImpl;
import com.entry.Goods;
import com.entry.Zgoods;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/Pformation/mation.do")
public class PMationServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        String ainto = req.getParameter("ainto");
        PMationDao pMationDao = new PMationImpl();
        int pageSize = req.getParameter("pageSize") == null ? 5 : Integer.parseInt(req.getParameter("pageSize").toString());
        int pageNo = req.getParameter("pageNo") == null ? 1 : Integer.parseInt(req.getParameter("pageNo").toString());
        if(ainto == null) {
            PageModel<Zgoods> pm = pMationDao.findSp(pageSize,pageNo);
            pm.setPageNav(req.getRequestURI());
            session.setAttribute("mp", pm);
            resp.sendRedirect("./Pmation.jsp");
        }else if("update1".equals(ainto)) {
            String id = req.getParameter("id");
            Zgoods zgoods = pMationDao.findId(id);
            session.setAttribute("zgoods", zgoods);
            resp.sendRedirect("./Pmation.jsp");
        }else if("update".equals(ainto)) {
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(req.getParameter("c"));//构造JsonObject对象
            int zid = Integer.parseInt(jt.get("zid").getAsString());
            String zname = jt.get("zname").getAsString();
            try{
                int zcount = Integer.parseInt(jt.get("zcount").getAsString());
                String gid = jt.get("gid").getAsString();
                String supname = jt.get("supname").getAsString();
                Zgoods zgoods = new Zgoods();
                zgoods.setZid(zid);
                zgoods.setZname(zname);
                zgoods.setZcount(zcount);
                zgoods.setGid(gid);
                zgoods.setSup_name(supname);
                if (pMationDao.update(zgoods) != 0) {
                    session.setAttribute("PmUpMess", "修改成功");
                } else {
                    session.setAttribute("PmUpMess", "修改失败");
                }
                session.setAttribute("zgoods", zgoods);
                PageModel<Zgoods> pm = pMationDao.findSp(pageSize,pageNo);
                pm.setPageNav(req.getRequestURI());
                session.setAttribute("mp", pm);
                resp.sendRedirect("./Pmation.jsp");
            }catch (NumberFormatException e){
                session.setAttribute("PmUpMess", "数值修改错误");
                resp.sendRedirect("./Pmation.jsp");
            }
        }else if("delete".equals(ainto)){
            String id = req.getParameter("id");
            if(pMationDao.delete(id) != 0){
                session.setAttribute("PmDpMess","删除成功");
            }else{
                session.setAttribute("PmDpMess","删除失败");
            }
            PageModel<Zgoods> pm = pMationDao.findSp(pageSize,pageNo);
            pm.setPageNav(req.getRequestURI());
            session.setAttribute("mp", pm);
            resp.sendRedirect("./Pmation.jsp");
        }
    }
}
